package com.dd.stack.mybatisplus.service.inner.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.stack.mybatisplus.config.exception.DomainException;
import com.dd.stack.mybatisplus.convertor.DataObjectConvertor;
import com.dd.stack.mybatisplus.mapper.DataObjectMapper;
import com.dd.stack.mybatisplus.pojo.DO.DataObjectDO;
import com.dd.stack.mybatisplus.pojo.VO.DataObjectVO;
import com.dd.stack.mybatisplus.pojo.VO.TypeEnumVO;
import com.dd.stack.mybatisplus.pojo.enumeration.DOTypeEnum;
import com.dd.stack.mybatisplus.pojo.req.DataObjectAddReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectQueryReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectUpdateReq;
import com.dd.stack.mybatisplus.service.inner.DataObjectService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/8 23:11
 * @Description DataObjectServiceImpl
 */
@Service
public class DataObjectServiceImpl extends ServiceImpl<DataObjectMapper, DataObjectDO> implements DataObjectService {

    @Override
    public void addOne(DataObjectAddReq req) {
        // 1 数据格式验证
        if(req.existEmptyData()) throw new DomainException(500, "请求数据存在空值，请检查。");
        // 1.2 数据唯一性校验（如果表设计有实体标识 实体标识是唯一的 则添加的时候需要校验是否已经存在 存在则不可添加）
        /* IF(baseMapper.selectList(req.get实体标识).szie() > 0) {
               throw new DomainException(500, "该实体标识已经存在");
           } */
        // 2 转换得到要插入的dataObjectDO对象
        DataObjectDO dataObjectDO = DataObjectConvertor.convert2DataObjectDO(req);
        // 3 执行插入并返回结果
        this.save(dataObjectDO);
    }

    @Override
    public void updateOne(DataObjectUpdateReq req) {
        // 初步得到要插入的dataObjectDO对象
        DataObjectDO DO = DataObjectConvertor.updateReq2DO(req);
        // 执行更新并返回结果
        this.updateById(DO);
    }

    @Override
    public IPage<DataObjectVO> pageQuery(DataObjectQueryReq req) {
        // 1 构建查询条件
        LambdaQueryWrapper<DataObjectDO> lambdaQueryWrapper = req.buildLambdaQueryWrapper();
        // 2 初始化Page对象
        Page<DataObjectDO> page = new Page<>(req.getPageParams().getPageNum(), req.getPageParams().getPageSize());
        // 3 获取DO分页结果
        Page<DataObjectDO> pageOfDO = this.page(page, lambdaQueryWrapper);
        // 4.1 DO列表转换VO
        List<DataObjectVO> dataObjectVOList = pageOfDO.getRecords().stream()
                .map(DataObjectConvertor::DO2VO)
                .collect(Collectors.toList());
        // 4.2 创建新的VO分页对象并返回
        Page<DataObjectVO> pageOfVO = new Page<>(req.getPageParams().getPageNum(), req.getPageParams().getPageSize());
        pageOfVO.setTotal(pageOfDO.getTotal());
        pageOfVO.setRecords(dataObjectVOList);
        return pageOfVO;
    }
}
